package com.efast.dmom.mgmt.service.mes.impl;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.efast.cafe.framework.bean.ResultBean;
import com.efast.cafe.portal.entity.common.PortalAttachmentBean;
import com.efast.cafe.portal.remote.common.PortalAttachmentService;
import com.efast.dmom.mgmt.bean.mes.ProductConfigAcceptBean;
import com.efast.dmom.mgmt.bean.mes.WorkOrderInfoDto;
import com.efast.dmom.mgmt.bean.mes.prodConfig.SopUploadBean;
import com.efast.dmom.mgmt.bean.mes.qm.QMCheckAreaBean;
import com.efast.dmom.mgmt.bean.mes.qm.QMCheckDefectCodeBean;
import com.efast.dmom.mgmt.bean.mes.qm.QMCheckItemBean;
import com.efast.dmom.mgmt.bean.mes.qm.QMCheckPartBean;
import com.efast.dmom.mgmt.dao.mes.TrBasProductConfigDao;
import com.efast.dmom.mgmt.entity.mes.TrBasPdPlRoute;
import com.efast.dmom.mgmt.entity.mes.TrBasProductConfig;
import com.efast.dmom.mgmt.service.mes.TrBasPdPlRouteService;
import com.efast.dmom.mgmt.service.mes.TrBasProductConfigService;
import com.efast.dmom.mgmt.utils.Assist;
@Service
public class TrBasProductConfigServiceImpl implements TrBasProductConfigService{
    @Autowired
    private TrBasProductConfigDao trBasProductConfigDao;
    @Autowired
    private TrBasPdPlRouteService trBasPdPlRouteService;
    
    @Autowired
	private PortalAttachmentService portalAttachmentService;
    
    @Override
    public long getTrBasProductConfigRowCount(Assist assist){
        return trBasProductConfigDao.getTrBasProductConfigRowCount(assist);
    }
    @Override
    public List<TrBasProductConfig> selectTrBasProductConfig(Assist assist){
        return trBasProductConfigDao.selectTrBasProductConfig(assist);
    }
    @Override
    public TrBasProductConfig selectTrBasProductConfigByObj(TrBasProductConfig obj){
        return trBasProductConfigDao.selectTrBasProductConfigByObj(obj);
    }
    @Override
    public TrBasProductConfig selectTrBasProductConfigById(Integer id){
        return trBasProductConfigDao.selectTrBasProductConfigById(id);
    }
    @Override
    public int insertTrBasProductConfig(TrBasProductConfig value){
        return trBasProductConfigDao.insertTrBasProductConfig(value);
    }
    @Override
    public int insertNonEmptyTrBasProductConfig(TrBasProductConfig value){
        return trBasProductConfigDao.insertNonEmptyTrBasProductConfig(value);
    }
    @Override
    public int insertTrBasProductConfigByBatch(List<TrBasProductConfig> value){
        return trBasProductConfigDao.insertTrBasProductConfigByBatch(value);
    }
    @Override
    public int deleteTrBasProductConfigById(Integer id){
        return trBasProductConfigDao.deleteTrBasProductConfigById(id);
    }
    @Override
    public int deleteTrBasProductConfig(Assist assist){
        return trBasProductConfigDao.deleteTrBasProductConfig(assist);
    }
    @Override
    public int updateTrBasProductConfigById(TrBasProductConfig enti){
        return trBasProductConfigDao.updateTrBasProductConfigById(enti);
    }
    @Override
    public int updateTrBasProductConfig(TrBasProductConfig value, Assist assist){
        return trBasProductConfigDao.updateTrBasProductConfig(value,assist);
    }
    @Override
    public int updateNonEmptyTrBasProductConfigById(TrBasProductConfig enti){
        return trBasProductConfigDao.updateNonEmptyTrBasProductConfigById(enti);
    }
    @Override
    public int updateNonEmptyTrBasProductConfig(TrBasProductConfig value, Assist assist){
        return trBasProductConfigDao.updateNonEmptyTrBasProductConfig(value,assist);
    }

    public TrBasProductConfigDao getTrBasProductConfigDao() {
        return this.trBasProductConfigDao;
    }

    public void setTrBasProductConfigDao(TrBasProductConfigDao trBasProductConfigDao) {
        this.trBasProductConfigDao = trBasProductConfigDao;
    }
	@Override
	public TrBasProductConfig getCfgConfig(WorkOrderInfoDto dto) {
		TrBasProductConfig cfg = null;
		Assist assist = new Assist();
		assist.setRequires(Assist.andEq("line_id", dto.getLineId()));
		assist.setRequires(Assist.andEq("part_no_version", dto.getPartNoVersion()));
		List<TrBasPdPlRoute> pdList = trBasPdPlRouteService.selectTrBasPdPlRoute(assist);
		if(pdList!=null&&pdList.size()>0) {
			Assist cfgAssist = new Assist();
			cfgAssist.setRequires(Assist.andEq("operation_id", dto.getOperationId()));
			cfgAssist.setRequires(Assist.andEq("route_id", pdList.get(0).getRouteId()));
			List<TrBasProductConfig> cfgList = trBasProductConfigDao.selectTrBasProductConfig(cfgAssist);
			if(cfgList!=null&&cfgList.size()>0) {
				cfg = cfgList.get(0);
			}
		}
		return cfg;
	}
	@Override
	public String getOperationIdByStationName(Integer stationId) {
		StringBuffer operationIds = new StringBuffer();
		String resultOperation = null;
		List<Integer> operationIdByStationName = trBasProductConfigDao.getOperationIdByStationName(stationId);
		if (operationIdByStationName.size() == 0) {
			operationIds.append(0);
		} else {
			operationIds.append(operationIdByStationName.get(0));
			for (int i = 1; i < operationIdByStationName.size(); i++) {
				operationIds.append("~"+operationIdByStationName.get(i));
			}
			resultOperation = operationIds.toString();
		}
		
		return resultOperation;
	}
	@Override
	public void updateIsQualityGate(Map<String, Object> map) {
		@SuppressWarnings("unchecked")
		Map<String,Object> map2=(Map<String, Object>) map.get("param");
		Boolean isQualityGate = (Boolean)map2.get("isQualityGate");
		Integer productConfigId = (Integer)map2.get("productConfigId");
		trBasProductConfigDao.updateIsQualityGate(isQualityGate,productConfigId);   
	}
	@Override
	public ResultBean selectQualityGate(Map<String,Object> map) {
		ResultBean resultBean = new ResultBean();
		@SuppressWarnings("unchecked")
		Map<String,Object> map2=(Map<String, Object>) map.get("selectData");
		Integer operationId = (Integer)map2.get("operationId");
		String partNoVersion = (String)map2.get("partNoVersion");
		Integer stationId = (Integer)map2.get("stationId");
		String routeName = (String)map2.get("route");
		String selectQualityGate = trBasProductConfigDao.selectQualityGate(operationId,stationId,routeName,partNoVersion);
		resultBean.setSuccess(true);
		resultBean.setObject(selectQualityGate);
		return resultBean;
	}
	
	// 多条件查询是否缺陷录入
	@Override
	public ResultBean selectDefectFlagFromSome(Map<String, Object> map) {
		ResultBean resultBean = new ResultBean();
		@SuppressWarnings("unchecked")
		Map<String,Object> map2=(Map<String, Object>) map.get("selectData");
		Integer operationId = (Integer)map2.get("operationId");
		String partNoVersion = (String)map2.get("partNoVersion");
		Integer stationId = (Integer)map2.get("stationId");
		String routeName = (String)map2.get("route");
		String selectQualityGate = trBasProductConfigDao.selectDefectFlagFromSome(operationId,stationId,routeName,partNoVersion);
		resultBean.setSuccess(true);
		resultBean.setObject(selectQualityGate);
		return resultBean;
	}
	
	@Override
	public ResultBean selectQualityGateByid(Map<String,Object> map) {
		ResultBean resultBean = new ResultBean();
		@SuppressWarnings("unchecked")
		Map<String,Object> map2=(Map<String, Object>) map.get("updateQuality");
		Integer productConfigId = (Integer)map2.get("productConfigId");
		String qualityGate = trBasProductConfigDao.selectQualityGateByid(productConfigId);
		if (qualityGate!=null) {
			resultBean.setSuccess(true);
			resultBean.setObject(qualityGate);
		} else {
			resultBean.setSuccess(false);
			resultBean.setMessage("无法查询到产品工序属性！");
		}
		return resultBean;
	}
	@Override
	public void updateDefectCodeFlag(Map<String, Object> map) {
		@SuppressWarnings("unchecked")
		Map<String,Object> map2=(Map<String, Object>) map.get("param");
		Boolean defectCodeFlag = (Boolean)map2.get("defectCodeFlag");
		Integer productConfigId = (Integer)map2.get("productConfigId");
		trBasProductConfigDao.updateDefectCodeFlag(productConfigId, defectCodeFlag);
	}
	@Override
	public ResultBean selectShowDefectSectionGateByid(Map<String, Object> map) {
		ResultBean resultBean = new ResultBean();
		@SuppressWarnings("unchecked")
		Map<String,Object> map2=(Map<String, Object>) map.get("defectSection");
		Integer productConfigId = (Integer)map2.get("productConfigId");
		String selectShowDefectSectionGateByid = trBasProductConfigDao.selectShowDefectSectionGateByid(productConfigId);
		if (selectShowDefectSectionGateByid!=null) {
			resultBean.setSuccess(true);
			resultBean.setObject(selectShowDefectSectionGateByid);
		} else {
			resultBean.setSuccess(false);
			resultBean.setMessage("无法查询到缺陷录入！");
		}
		return resultBean;
	}
	@Override
	public ResultBean selectShowInAndOutSectionGateByid(Map<String, Object> map) {
		ResultBean resultBean = new ResultBean();
		@SuppressWarnings("unchecked")
		Map<String,Object> map2=(Map<String, Object>) map.get("InAndOut");
		Integer productConfigId = (Integer)map2.get("productConfigId");
		String selectShowInAndOutSectionGateByid = trBasProductConfigDao.selectShowInAndOutSectionGateByid(productConfigId);
		if (selectShowInAndOutSectionGateByid!=null) {
			resultBean.setSuccess(true);
			resultBean.setObject(selectShowInAndOutSectionGateByid);
		} else {
			resultBean.setSuccess(false);
			resultBean.setMessage("无法查询到拉入拉出！");
		}
		return resultBean;
	}
	@Override
	public ResultBean selectIsAutoStartSectionGateByid(Map<String, Object> map) {
		ResultBean resultBean = new ResultBean();
		@SuppressWarnings("unchecked")
		Map<String,Object> map2=(Map<String, Object>) map.get("autoStart");
		Integer productConfigId = (Integer)map2.get("productConfigId");
		String selectIsAutoStartSectionGateByid = trBasProductConfigDao.selectIsAutoStartSectionGateByid(productConfigId);
		if (selectIsAutoStartSectionGateByid!=null) {
			resultBean.setSuccess(true);
			resultBean.setObject(selectIsAutoStartSectionGateByid);
		} else {
			resultBean.setSuccess(false);
			resultBean.setMessage("无法查询到自动开始配置！");
		}
		return resultBean;
	}
	@Override
	public ResultBean selectIsCertificateSectionGateByid(Map<String, Object> map) {
		ResultBean resultBean = new ResultBean();
		@SuppressWarnings("unchecked")
		Map<String,Object> map2=(Map<String, Object>) map.get("certificateData");
		Integer productConfigId = (Integer)map2.get("productConfigId");
		String selectIsCertificateSectionGateByid = trBasProductConfigDao.selectIsCertificateSectionGateByid(productConfigId);
		if (selectIsCertificateSectionGateByid!=null) {
			resultBean.setSuccess(true);
			resultBean.setObject(selectIsCertificateSectionGateByid);
		} else {
			resultBean.setSuccess(false);
			resultBean.setMessage("无法查询到合格证编号配置！");
		}
		return resultBean;
	}
	@Override
	public List<QMCheckPartBean> selectAllBomItemPart(String partNoVersion) {
		List<QMCheckPartBean> selectAllBomItemPart = trBasProductConfigDao.selectAllBomItemPart(partNoVersion);
		return selectAllBomItemPart;
	}
	@Override
	public List<QMCheckAreaBean> selectAllArea() {
		List<QMCheckAreaBean> selectAllArea = trBasProductConfigDao.selectAllArea();
		return selectAllArea;
	}
	@Override
	public List<QMCheckItemBean> selectItemfromShop(Integer stationId) {
		List<QMCheckItemBean> selectItemfromShop = trBasProductConfigDao.selectItemfromShop(stationId);
		return selectItemfromShop;
	}
	@Override
	public List<QMCheckDefectCodeBean> selectDefectcodefromItem(Map<String, Object> map) {
		String itemName = (String)map.get("value");
		String workshopNo = (String)map.get("workShopNo");
		List<QMCheckDefectCodeBean> selectDefectcodefromItem = trBasProductConfigDao.selectDefectcodefromItem(itemName,workshopNo);
		return selectDefectcodefromItem;
	}

	@Override
	public void uploadSop(SopUploadBean sopBean) {
		
		portalAttachmentService.saveAttachmentForm(sopBean.getFormId(), sopBean.getAttachments());
		
		trBasProductConfigDao.saveAttachFormId(sopBean.getProductConfigId(), sopBean.getFormId());
	}
	@Override
	public int getAttachId(int routeId, int operationId) {
		//TODO one station ---only one configuration ????
		List<String> result = trBasProductConfigDao.getGenerateFromId(routeId, operationId);
		
		int attachmentId = -1;
		
		if(result != null && result.size() >0){
			
			List<PortalAttachmentBean> attachBeanList = portalAttachmentService.queryAttachmentFormListByFormId(result.get(0));
			if(attachBeanList != null && attachBeanList.size() > 0){
				
				attachmentId = attachBeanList.get(0).getId();
			}
		}
		return attachmentId;
	}
	@Override
	public void updateIsInAndOut(Map<String, Object> map) {
		@SuppressWarnings("unchecked")
		Map<String,Object> map2=(Map<String, Object>) map.get("param");
		Boolean isInPullOut = (Boolean)map2.get("isInPullOut");
		Integer productConfigId = (Integer)map2.get("productConfigId");
		trBasProductConfigDao.updateIsInAndOut(isInPullOut,productConfigId);  		
	}
	public String getFormIdByProductConfigId(Integer productConfigId) {
		
		return trBasProductConfigDao.getFormIdByProductConfigId(productConfigId);
	}
	@Override
	public void updateIsAutoStart(Map<String, Object> map) {
		@SuppressWarnings("unchecked")
		Map<String,Object> map2=(Map<String, Object>) map.get("param");
		Boolean isAutoStart = (Boolean)map2.get("isAutoStart");
		Integer productConfigId = (Integer)map2.get("productConfigId");
		trBasProductConfigDao.updateIsAutoStart(isAutoStart,productConfigId);  		
	}
	
	@Override
	public void updateIsCertificate(Map<String, Object> map) {
		@SuppressWarnings("unchecked")
		Map<String,Object> map2=(Map<String, Object>) map.get("param");
		Boolean isCertificate = (Boolean)map2.get("isCertificate");
		Integer productConfigId = (Integer)map2.get("productConfigId");
		trBasProductConfigDao.updateIsCertificate(isCertificate,productConfigId);  	
	}

	@Override
	public ResultBean selectSpliceByid(Integer id) {
		ResultBean resultBean = new ResultBean();
		if (id != null) {
			Integer isSplice = trBasProductConfigDao.selectSpliceByid(id);
			if (isSplice != null) {
				resultBean.setSuccess(true);
				resultBean.setObject(isSplice);
			} else {
				resultBean.setSuccess(false);
				resultBean.setMessage("查询不到对应的合拼信息");
			}
		} else {
			resultBean.setSuccess(false);
			resultBean.setMessage("传入的参数为空");
		}
		return resultBean;
	}
	@Override
	public ResultBean saveIsSplice(Integer productConfigId, Integer isSplice) {
		ResultBean resultBean = new ResultBean();
		Integer saveIsSplice = trBasProductConfigDao.saveIsSplice(productConfigId, isSplice);
		if (saveIsSplice == 1) {
			resultBean.setSuccess(true);
			resultBean.setMessage("保存成功！");
		} else {
			resultBean.setSuccess(false);
			resultBean.setMessage("保存失败！");
		}
		return resultBean;
	}
	@Override
	public void updateIsVinBinding(Map<String, Object> map) {
		@SuppressWarnings("unchecked")
		Map<String,Object> map2=(Map<String, Object>) map.get("param");
		Boolean isVinBinding = (Boolean)map2.get("isVinBinding");
		Integer productConfigId = (Integer)map2.get("productConfigId");
		trBasProductConfigDao.updateIsVinBinding(isVinBinding,productConfigId); 
		
	}
	@Override
	public ResultBean selectIsVinBindingSectionGateByid(Map<String, Object> map) {
		ResultBean resultBean = new ResultBean();
		@SuppressWarnings("unchecked")
		Map<String,Object> map2=(Map<String, Object>) map.get("vinBindingData");
		Integer productConfigId = (Integer)map2.get("productConfigId");
		String selectIsVinBindingSectionGateByid = trBasProductConfigDao.selectIsVinBindingSectionGateByid(productConfigId);
		if (selectIsVinBindingSectionGateByid!=null) {
			resultBean.setSuccess(true);
			resultBean.setObject(selectIsVinBindingSectionGateByid);
		} else {
			resultBean.setSuccess(false);
			resultBean.setMessage("无法查询到vin绑定配置！");
		}
		return resultBean;
	}
	
	//配置项更新
	@Override
	public void updateConfigurationItem(ProductConfigAcceptBean productConfigAcceptBean) {
		if(productConfigAcceptBean.getIsShowHeadStation()==null) {
		trBasProductConfigDao.updateConfigurationItemByNull(productConfigAcceptBean);
		} else {
		trBasProductConfigDao.updateConfigurationItem(productConfigAcceptBean);
		}
	}
	//为true时是否标识为首工位
	@Override
	public ResultBean updateIsShowHeadStation(Map<String, Object> map) {
		ResultBean resultBean = new ResultBean(true,"'");
		@SuppressWarnings("unchecked")
		Map<String,Object> map2=(Map<String, Object>) map.get("param");
		Boolean isShowHeadStation = (Boolean)map2.get("isShowHeadStation");
		Integer productConfigId = (Integer)map2.get("productConfigId");
		String routeName=trBasProductConfigDao.selectRouteNameByProductConfigId(productConfigId);
		WorkOrderInfoDto workOrderInfoDto = trBasProductConfigDao.selectWorkOrderInfoDto(routeName);
		if(workOrderInfoDto!=null && !workOrderInfoDto.getProductConfigId().equals(productConfigId)) {
			resultBean.setSuccess(false);
			resultBean.setMessage("已存在工位被标识为首工位，其为"+routeName+"-"+workOrderInfoDto.getOperationName()+"-"+workOrderInfoDto.getStationName());
		} else {
			trBasProductConfigDao.updateIsShowHeadStation(isShowHeadStation,productConfigId);
			resultBean.setSuccess(true);
			resultBean.setMessage("更新成功！");
		}
		return resultBean;
	}
	@Override
	public ResultBean selectHeadStationByid(Map<String, Object> map) {
		ResultBean resultBean = new ResultBean();
		@SuppressWarnings("unchecked")
		Map<String,Object> map2=(Map<String, Object>) map.get("headStation");
		Integer productConfigId = (Integer)map2.get("productConfigId");
		String headStation = trBasProductConfigDao.selectHeadStationByid(productConfigId);
		if (headStation!=null) {
			resultBean.setSuccess(true);
			resultBean.setObject(headStation);
		} else {
			resultBean.setSuccess(false);
			resultBean.setMessage("无法查询到首工位！");
		}
		return resultBean;
	}
	}
